/*-------------------------------------------------------------------------

***************** Modification History ***********************************
   @doc
   @module opedit.c - Table edit operations |
   @end

$Log: /SoftPay/SPK2SIM/Source/Main/opedit.c $
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#define VSOPEDIT_C

#include <aclconio.h>
#include <aclstr.h>
#include <stdio.h>
#include <stdlib.h>
#include <applidl.h>
#include <svctxo.h>
#include <dbmgr.h>
#include <string.h>

#include <project.h>

/** /#define DEBUG_VSOPEDIT */
#ifdef DEBUG_VSOPEDIT
#define DEBUG_95
#endif
/**/

#include <define.h>
#include <transact.h>
#include <date.h>
#include <pscalls.h>
#include <ui.h>
#include <modem.h>
#include <msg.h>
#include <msgfuncs.h>
#include <spdebug.h>
#include <bdt.h>
#include <cdt.h>
#include <tblutil.h>
#include <dpt.h>
#include <fct.h>
#include <hdt.h>
#include <ict.h>
#include <lct.h>
#include <opt.h>
#include <tct.h>
#include <pct.h>
#include <pdt.h>
#include <cct.h>
#include <udt.h>
#include <sct.h>
#include <mit.h>
#include <mht.h>
#include <sdt.h>
#include <opedit.h>
#include <file.h>
#include <test.h>
#include <confio.h>
#include <codesec.h>
#include <fleet.h>

/* --------------------------------------------------------------------------*/

#define TEMP_CDT_FILE_NAME "cdt.TMP"
#define TEMP_MHT_FILE_NAME "mht.TMP"

/*********************************************************************************************
* @func int | inEditLDTOperation |
*       Edit the LDT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditLDTOperation(TRANSACTION_OBJECT* pobTran) {
    inConfRecEdit("LDT");
    return(BRANCH_EXIT);
}

/*********************************************************************************************
* @func int | inEditCDTOperation |
*       Edit the CDT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/

int inEditCDTOperation(TRANSACTION_OBJECT* pobTran) {
    char szFileName[FILENAME_SIZE + 1];
    VS_BOOL fFileExists = VS_FALSE;

    vdGetCDTFileName(szFileName);
    if (strcmp(szFileName, CDT_FILENAME) != 0) {
        /* if CDT file already exists, rename */
        if (inChkFileExist(CDT_FILENAME) == VS_SUCCESS)
            rename(CDT_FILENAME, TEMP_CDT_FILE_NAME);
        rename(szFileName, CDT_FILENAME);
        fFileExists = VS_TRUE;
    }

    if (inChkFileExist(CDT_FILENAME) == VS_SUCCESS)
        inConfRecEdit("CDT");
    else
        vdDisplayErrorMessage(CDT_ERR_MSG21);

    if (fFileExists) {
        rename(CDT_FILENAME, szFileName);
        if (inChkFileExist(TEMP_CDT_FILE_NAME) == VS_SUCCESS)
            rename(TEMP_CDT_FILE_NAME, CDT_FILENAME);
    }

    return(BRANCH_EXIT);
}


/*********************************************************************************************
* @func int | inEditCPTOperation |
*       Edit the CPT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditCPTOperation(TRANSACTION_OBJECT* pobTran) {
#ifdef MODEM
    inConfRecEdit("CPT");
#endif
    return(BRANCH_EXIT);
}


/*********************************************************************************************
* @func int | inEditHDTOperation |
*       Edit the HDT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditHDTOperation(TRANSACTION_OBJECT* pobTran) {
    inConfRecEdit("HDT");
    if (inLoadHDTRec(0) != VS_SUCCESS)
        vdDisplayMessageFromFile(HDT_ERR_MSG21, ERROR_MSG);

    return(BRANCH_EXIT);
}


/*********************************************************************************************
* @func int | inEditTCTOperation |
*       Edit the TCT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditTCTOperation(TRANSACTION_OBJECT* pobTran) {
    VS_BOOL fTempMegaPW;

    /** /pdebug(("--inEditTCTOperation--"));*/
    inConfRecEdit("TCT");
    fTempMegaPW = fGetMegaPassword();
    vdSetMegaPassword(VS_FALSE);
    if (inLoadTCTRec(0) != VS_SUCCESS)
        vdDisplayMessageFromFile(TCT_ERR_MSG21, ERROR_MSG);
    vdSetMegaPassword(fTempMegaPW);

    return(BRANCH_EXIT);
}

/*********************************************************************************************
* @func int | inEditHFTOperation |
*       Edit the HFT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditHFTOperation(TRANSACTION_OBJECT* pobTran) {
    char szHostFileName[FILE_SUFFIX_SIZE + 1];

    /**/pdebug(("--inEditHFTOperation--"));/**/

    if (inGetHFTHostNum(pobTran) >= 0) {
        strcpy(szHostFileName, szGetHostFileName());
        inConfRecEdit(szHostFileName);
    }

    return(BRANCH_EXIT);
}


/*********************************************************************************************
* @func int | inEditBDTOperation |
*       Edit the BDT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditBDTOperation(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inEditBDTOperation--"));*/
    inConfRecEdit("BDT");

    return(BRANCH_EXIT);
}


/*********************************************************************************************
* @func int | inEditCCTOperation |
*       Edit the check reader configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditCCTOperation(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inEditCCTOperation--"));*/
    inConfRecEdit("CCT");

    return(BRANCH_EXIT);
}



/*********************************************************************************************
* @func int | inEditCRTOperation |
*       Edit the CR600 check reader configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditCRTOperation(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inEditCRTOperation--"));*/
    inConfRecEdit("CRT");

    return(BRANCH_EXIT);
}

/*-------------------------------------------------------------------------
    Function :  inEditICTOperation()
    Description : Edit the ISDN configuration table
    Parameters :
    Returns : BRANCH_EXIT
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   07/01/97 Kevin_V1   Creation...
--------------------------------------------------------------------------*/
/*********************************************************************************************
* @func int | inEditICTOperation |
*       Edit the ICT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditICTOperation(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inEditICTOperation--"));*/
    inConfRecEdit("ICT");

    return(BRANCH_EXIT);
}

/*********************************************************************************************
* @func int | inEditDPTOperation |
*       Edit the Datpac 3201 configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditDPTOperation(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inEditDPTOperation--"));*/
    inConfRecEdit("DPT");

    return(BRANCH_EXIT);
}

/*********************************************************************************************
* @func int | inEditFCTOperation |
*       Edit the FCT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditFCTOperation(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inEditFCTOperation--"));*/
    inConfRecEdit("FCT");
    inProcessFCT(pobTran);

    return(BRANCH_EXIT);
}

/*********************************************************************************************
* @func int | inEditMITOperation |
*       Edit the MIT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/

int inEditMITOperation(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inEditMITOperation--"));*/
    inConfRecEdit("MIT");

    return(BRANCH_EXIT);
}

/*********************************************************************************************
* @func int | inEditMHTOperation |
*       Edit the MHT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/

int inEditMHTOperation(TRANSACTION_OBJECT* pobTran) {
    char szFileName[FILENAME_SIZE + 1];
    VS_BOOL fFileExists = VS_FALSE;

    vdGetMHTFileName(szFileName);
    if (strcmp(szFileName, MHT_FILENAME) != 0) {
        /* if MHT file already exists, rename */
        if (inChkFileExist(MHT_FILENAME) == VS_SUCCESS)
            rename(MHT_FILENAME, TEMP_MHT_FILE_NAME);
        rename(szFileName, MHT_FILENAME);
        fFileExists = VS_TRUE;
    }

    if (inChkFileExist(MHT_FILENAME) == VS_SUCCESS)
        inConfRecEdit("MHT");
    else
        vdDisplayErrorMessage(MHT_ERR_MSG21);

    if (fFileExists) {
        rename(MHT_FILENAME, szFileName);
        if (inChkFileExist(TEMP_MHT_FILE_NAME) == VS_SUCCESS)
            rename(TEMP_MHT_FILE_NAME, MHT_FILENAME);
    }

    return(BRANCH_EXIT);
}


/*********************************************************************************************
* @func int | inEditPCTOperation |
*       Edit the PCT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditPCTOperation(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inEditPCTOperation--"));*/
    inConfRecEdit("PCT");
    if (inLoadPCTRec(0) != VS_SUCCESS)
        vdDisplayMessageFromFile(PCT_ERR_MSG, ERROR_MSG);

    return(BRANCH_EXIT);
}



/*********************************************************************************************
* @func int | inEditPDTOperation |
*       Edit the PDT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/

#ifdef FLEET_DEFINED

int inEditPDTOperation(TRANSACTION_OBJECT* pobTran) {
    char szScratch[PDT_AMT_SIZE + 1];
    int x;

    /** /pdebug(("--inEditPDTOperation--"));*/

    inConfRecEdit("PDT");

    x = 0;
    /*
     * Go through the PDT records and insert the decimal point where it is appropriate.
    */
    while (inLoadPDTRec(x) == VS_SUCCESS) {
        memset(szScratch, 0x00, sizeof(szScratch));

        strlcpy(szScratch, szGetProductPrice(), PDT_AMT_SIZE);

        purge_char(szScratch, '.');

        while (strlen(szScratch) < ((fFuelCode(szGetProductCode()) == VS_TRUE) ? 4 : 3))
            insert_char(szScratch, 0, '0');

        insert_char(szScratch, strlen(szScratch) - ((fFuelCode(szGetProductCode()) == VS_TRUE) ? 3 : 2), '.');

        vdSetProductPrice(szScratch);

        inSavePDTRec(x);

        x++;
    }
    if (inLoadPDTRec(0) != VS_SUCCESS)
        vdDisplayMessageFromFile(PDT_ERROR_MSG, ERROR_MSG);

    return(BRANCH_EXIT);
}

#endif

/*********************************************************************************************
* @func int | inEditPMTOperation |
*       Edit the PMT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditPMTOperation(TRANSACTION_OBJECT* pobTran) {
    inConfRecEdit("PMT");

    return(BRANCH_EXIT);
}

/*********************************************************************************************
* @func int | inEditIITOperation |
*       Edit the IIT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditIITOperation(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inEditIITOperation--"));*/
    inConfRecEdit("IIT");

    return(BRANCH_EXIT);
}

/*********************************************************************************************
* @func int | inEditPITOperation |
*       Edit the PIT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditPITOperation(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inEditPITOperation--"));*/
    inConfRecEdit("PIT");

    return(BRANCH_EXIT);
}

/*********************************************************************************************
* @func int | inEditMCTOperation |
*       Edit the MCT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditMCTOperation(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inEditMCTOperation--"));*/
    inConfRecEdit("MCT");

    return(BRANCH_EXIT);
}


/*********************************************************************************************
* @func int | inEditAATOperation |
*       Edit the AAT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditAATOperation(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inEditAATOperation--"));*/
    inConfRecEdit("AAT");

    return(BRANCH_EXIT);
}


/*********************************************************************************************
* @func int | inEditISTOperation |
*       Edit the IST (ISO 8583) configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditISTOperation(TRANSACTION_OBJECT* pobTran) {
    inConfRecEdit("IST");

    return(BRANCH_EXIT);
}

/*********************************************************************************************
* @func int | inEditCNTOperation |
*       Edit the CNT (Counter) configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
#ifdef COUNTER_DEFINED
int inEditCNTOperation(TRANSACTION_OBJECT* pobTran) {
    inConfRecEdit("CNT");

    return(BRANCH_EXIT);
}
#endif/*COUNTER_DEFINED*/



int OPEDIT_CODE_CHECK(char* a,char* b,short* c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);
    return 1;
}
#ifdef __arm

char* strNumCopy(char* str1,const char* str2,int num) {
    while (*str2 != '\0' && num-- > 0)
        *(str1++) = *(str2++);
    *str1 = '\0';
    return str1;
}
#endif /* __arm */

#ifdef EMV_MODULE

/*********************************************************************************************
* @func int | inEditMVTOperation |
*       Edit the MVT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditMVTOperation(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inEditIITOperation--"));*/
    inConfRecEdit("MVT");

    return(BRANCH_EXIT);
}

#endif //EMV_MODULE
#ifdef VXEMVAPP

/*********************************************************************************************
* @func int | inEditMVTOperation |
*       Edit the MVT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditMVTOperation (TRANSACTION_OBJECT *pobTran)
{

    /*pdebug(("--inEditIITOperation--")); */
    inConfRecEdit("MVT");

    return(BRANCH_EXIT);
}

/*********************************************************************************************
* @func int | inEditESTOperation |
*       Edit the MVT configuration table
*
* @parm TRANSACTION_OBJECT * |pobTran |
*       Transaction object.
*
* @rdesc return values.
* @flag int    |  BRANCH_EXIT
* @end
**********************************************************************************************/
int inEditESTOperation (TRANSACTION_OBJECT *pobTran)
{

    /* pdebug(("--inEditIITOperation--")); */
    inConfRecEdit("EST");

    return(BRANCH_EXIT);
}

#endif //VXEMVAPP
